package com.hzya.frame.web.aop;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;

import java.util.Arrays;

/**
 * @description: 系统错误日志记录
 * @author: gjh
 * @Date Created in 2018/5/24 19:33
 */
@Aspect
public class AOPSystemErrorLogRecord {
    protected Logger logger = LogManager.getLogger(getClass());

    /**
     * 拦截方法
     */
    @Pointcut("execution(* com.hzya.frame..controller.*.*(..)) ")
    public void bindPoint() {
    }

    ;
    /**
     * 不需要拦截的方法
     */
    private String execMethods;

    @Around("bindPoint()")
    public Object process(ProceedingJoinPoint  point) throws Throwable {
//        System.out.println("@Around：执行目标方法之前...");
//        //访问目标方法的参数：
//        Object[] args = point.getArgs();
//        if (args != null && args.length > 0 && args[0].getClass() == String.class) {
//            args[0] = "改变后的参数1";
//        }
//        //用改变后的参数执行目标方法
//        Object returnValue = point.proceed(args);
//        System.out.println("@Around：执行目标方法之后...");
//        System.out.println("@Around：被织入的目标对象为：" + point.getTarget());
//        System.out.println("原返回值：" + returnValue + "，这是返回结果的后缀");

        return  point.proceed();
    }

    @Before("bindPoint()")
    public void permissionCheck(JoinPoint point) {
//        System.out.println("@Before：模拟权限检查...");
//        System.out.println("@Before：目标方法为：" +
//                point.getSignature().getDeclaringTypeName() +
//                "." + point.getSignature().getName());
//        System.out.println("@Before：参数为：" + Arrays.toString(point.getArgs()));
//        System.out.println("@Before：被织入的目标对象为：" + point.getTarget());
    }

    @AfterReturning(pointcut = "bindPoint()", returning = "returnValue")
    public void log(JoinPoint point, Object returnValue) {
//        System.out.println("@AfterReturning：模拟日志记录功能...");
//        System.out.println("@AfterReturning：目标方法为：" + point.getSignature().getDeclaringTypeName() + "." + point.getSignature().getName());
//        System.out.println("@AfterReturning：参数为：" + Arrays.toString(point.getArgs()));
//        System.out.println("@AfterReturning：返回值为：" + returnValue);
//        System.out.println("@AfterReturning：被织入的目标对象为：" + point.getTarget());

    }

    @After("bindPoint()")
    public void releaseResource(JoinPoint point) {
//        System.out.println("@After：模拟释放资源...");
//        System.out.println("@After：目标方法为：" +
//                point.getSignature().getDeclaringTypeName() +
//                "." + point.getSignature().getName());
//        System.out.println("@After：参数为：" + Arrays.toString(point.getArgs()));
//        System.out.println("@After：被织入的目标对象为：" + point.getTarget());
    }


//    @Around("bindPoint()")
//    public Object runRound(ProceedingJoinPoint joinPoint) throws Throwable {
//
//        String className = joinPoint.getTarget().getClass().getName();
//        String methodName = joinPoint.getSignature().getName();
//        logger.warn(className + "的" + methodName + "执行了");
//        Object[] args = joinPoint.getArgs();
//        StringBuilder log = new StringBuilder("入参为");
//        for (Object arg : args) {
//            log.append(arg + " ");
//        }
//        logger.warn(log.toString());
//
//        Object result=null;
//        Object target= joinPoint.getThis();
//        Object[] params=joinPoint.getArgs();
//        if(null!=params && 0< params.length){
//            Object param=params[0];
//            if(param instanceof BaseEntity){
//                try {
//                    String [] execMethodsAry= execMethods.split(",");
//                    for(int i=0;i<execMethodsAry.length;i++){
//                        BeanHelper.invoke(target, execMethodsAry[i], new Object[]{param});
//                    }
//                } catch (Exception e) {
//                    /** 如果出现异常，记录文件*/
//                    String  aaa =  target.getClass()+"";
//                    String bbb =  target.getClass().getName();
//                    String ccc=  target.getClass().toString();
//                    Signature signature =   joinPoint.getSignature();
//                    String actiona = target.getClass()+"===="+joinPoint.getSignature().getName();
//                    logger.error( e.getCause());
//                }finally {
//                    result=joinPoint.proceed();
//                }
//            }else{
//                result=  joinPoint.proceed();
//            }
//        }else{
//
//            result= joinPoint.proceed();
//        }
//        return result;
//    }

    public String getExecMethods() {
        return execMethods;
    }

    public void setExecMethods(String execMethods) {
        this.execMethods = execMethods;
    }


}
